package com.woniuxy.woniumall.controller;

import com.woniuxy.woniumall.entity.Address;
import com.woniuxy.woniumall.entity.User;
import com.woniuxy.woniumall.service.AddressServcie;
import com.woniuxy.woniumall.vo.CartVo;
import com.woniuxy.woniumall.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("address")
public class AddressController {

    @Autowired
    AddressServcie addressServcie ;

    @GetMapping("/myAddress")
    @ResponseBody
    public ResponseResult<List<Address>> findMyAddress(HttpSession session){
        ResponseResult<List<Address>> result = new ResponseResult<>();
        try {
            User user = (User) session.getAttribute("userInfo");
            List<Address> list = addressServcie.findMyAddress(user);
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(list);
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result ;
    }

    @PostMapping("/add")
    @ResponseBody
    public ResponseResult<Integer> add(HttpSession session,@RequestBody Address address){
        ResponseResult<Integer> result = new ResponseResult<>();
        try {
            User user = (User) session.getAttribute("userInfo");
            address.setUserId(user.getId());
            address.setStatus(1);
            address = addressServcie.add(address);
            result.setCode(200);
            result.setMsg("保存成功!");
            result.setData(address.getId());
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("保存失败!");
        }
        return result ;
    }

}
